package com.ruoyi.wechat.api;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.wechat.domain.Order;
import com.ruoyi.wechat.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.io.File;
import static com.ruoyi.common.core.domain.AjaxResult.success;

@RestController
@RequestMapping("/qr-code")
public class QRCodeController {
    @Autowired
    private IOrderService orderService;

    // 生成二维码并返回二维码的URL地址
    @PostMapping("/generate/{orderId}")
    public AjaxResult generateQRCode(@PathVariable String orderId) throws Exception {
        System.out.println("二维码字符"+orderId);
            // 生成唯一的二维码文件名
            String uploadFilePath = RuoYiConfig.getUploadPath();
            String fileName = System.currentTimeMillis() + ".png";  // 使用时间戳生成文件名
            String filePath = uploadFilePath + File.separator + fileName;  // 存储路径
            // 生成二维码并保存到文件
            QRCodeGenerator.generateQRCode(orderId, 300, 300, filePath);
             Order order = new Order();
             order.setOrderId(orderId);
             String qrcode = "/profile/upload/" + fileName;
            System.out.println("二维码地址："+qrcode);
            order.setStatus("0");
             order.setQrCode(qrcode);
        System.out.println(order.getStatus());
            orderService.updateOrder(order);
            // 返回二维码的URL地址
            return success(orderService.updateOrder(order));
    }
}
